package com.kinon.gfcanteen_v1.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.kinon.gfcanteen_v1.R;
import com.kinon.gfcanteen_v1.bean.BaseBean;
import com.kinon.gfcanteen_v1.network.CommonSubscriber;
import com.kinon.gfcanteen_v1.network.HttpManage;
import com.kinon.gfcanteen_v1.network.StrCallback;
import com.kinon.gfcanteen_v1.util.CustomDialog;
import com.kinon.gfcanteen_v1.util.RxUtil;
import com.kinon.gfcanteen_v1.util.SPManager;
import com.kinon.gfcanteen_v1.view.activity.LoginActivity;
import com.kinon.gfcanteen_v1.view.activity.PayActivity;
import com.kinon.gfcanteen_v1.view.base.BaseFragment;

import io.reactivex.disposables.Disposable;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2018-01-08.
 */

public class SettelmentFragment extends BaseFragment implements View.OnClickListener{

    private View mView;
    private TextView tv_title;
    private TextView tv_out;
    private TextView tv_totalPrice;
    private Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,
            btn_7,btn_8,btn_9,btn_d,btn_c,btn_x,btn_complete;

    private String strTotal="";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView=inflater.inflate(R.layout.fragemtn_settelment_layout,container,false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        tv_title=mView.findViewById(R.id.tv_title);
        tv_out=mView.findViewById(R.id.tv_out);
        tv_totalPrice=mView.findViewById(R.id.tv_totalPrice);
        btn_0=mView.findViewById(R.id.btn_0);
        btn_1=mView.findViewById(R.id.btn_1);
        btn_2=mView.findViewById(R.id.btn_2);
        btn_3=mView.findViewById(R.id.btn_3);
        btn_4=mView.findViewById(R.id.btn_4);
        btn_5=mView.findViewById(R.id.btn_5);
        btn_6=mView.findViewById(R.id.btn_6);
        btn_7=mView.findViewById(R.id.btn_7);
        btn_8=mView.findViewById(R.id.btn_8);
        btn_9=mView.findViewById(R.id.btn_9);
        btn_d=mView.findViewById(R.id.btn_d);
        btn_c=mView.findViewById(R.id.btn_c);
        btn_x=mView.findViewById(R.id.btn_x);
        btn_complete=mView.findViewById(R.id.btn_complete);
        tv_title.setText("结算");
        tv_out.setVisibility(View.VISIBLE);
        addListener();
    }
    private void addListener(){
        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_d.setOnClickListener(this);
        btn_c.setOnClickListener(this);
        btn_x.setOnClickListener(this);
        btn_complete.setOnClickListener(this);
        tv_out.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_out:
                CustomDialog dialog=new CustomDialog(getActivity(),"是否退出登录",
                        new CustomDialog.DialogClickListener() {
                    @Override
                    public void onDialogClick(int btn) {
                      if(btn==1){
                          SPManager.getInstance().removesp();
                       Intent intent=new Intent(getActivity(), LoginActivity.class);
                          startActivity(intent);
                          getActivity().finish();
                      }
                    }
                });
                dialog.show();
            break;
            case R.id.btn_0:
                strTotal=strTotal+"0";
            break;
            case R.id.btn_1:
                strTotal=strTotal+"1";
            break;
            case R.id.btn_2:
                strTotal=strTotal+"2";
                break;
            case R.id.btn_3:
                strTotal=strTotal+"3";
                break;
            case R.id.btn_4:
                strTotal=strTotal+"4";
                break;
            case R.id.btn_5:
                strTotal=strTotal+"5";
                break;
            case R.id.btn_6:
                strTotal=strTotal+"6";
                break;
            case R.id.btn_7:
                strTotal=strTotal+"7";
                break;
            case R.id.btn_8:
                strTotal=strTotal+"8";
                break;
            case R.id.btn_9:
                strTotal=strTotal+"9";
                break;
            case R.id.btn_d:
                if(strTotal.contains(".")){
                    return;
                }else{
                    strTotal+=".";
                }
                break;
            case R.id.btn_c:
                strTotal="";
                break;
            case R.id.btn_x:
                if(strTotal.length()>1){
                    strTotal=strTotal.substring(0,strTotal.length()-1);
                }else{
                    strTotal="";
                }
                break;
            case R.id.btn_complete:
                if(TextUtils.isEmpty(strTotal)){
                  return;
                }
                saveNewOrder(strTotal);
                strTotal="";
                break;
        }
        tv_totalPrice.setText(strTotal);
    }
    private void saveNewOrder(final  String total){
        final String nonce_str=System.currentTimeMillis()+"";
        Call<String> call=HttpManage.getInstance().saveNewOrder
                (SPManager.getInstance().getToken(),total,10,nonce_str);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                Intent intent=new Intent(getActivity(), PayActivity.class);
                intent.putExtra("orderid",data);
                intent.putExtra("total",total);
                intent.putExtra("nonce_str",nonce_str);
                startActivity(intent);
            }

            @Override
            public void onError(int code, String error) {
                mToast(error);
            }
        });
    }
}
